home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 12 / BBS in a box XII-2.iso / Files II / Prog / T / TIFF Code.sit / compress.c / compress.c
Encoding:
C/C++ Source or Header  |  1987-12-16  |  10.8 KB  |  393 lines  |  [TEXT/MPS ]

  1. /*
  2.  * compress.c
  3.  *
  4.  * Copyright (c) 1987 by Bear River Associates, Inc.
  5.  */
  6.  
  7.  
  8. /* Primary Interface Files */
  9. #include "Types.h"
  10. #include "Resources.h"
  11. #include "Quickdraw.h"
  12. #include "Windows.h"
  13. #include "OSUtils.h"
  14.  
  15. #include "ToolUtils.h"
  16. #include "TextEdit.h"
  17. #include "Controls.h"
  18.  
  19. /* Application-specific Include files */
  20. #include "tifflib.h"
  21.  
  22. struct CodeStr
  23.     {
  24.     Int16 runlength;
  25.     Int16 codelength;
  26.     UInt16 pattern;
  27.     };
  28.  
  29. #define entry(a, b, c, d) a,b,(d<<(16 - b))
  30.  
  31. static struct CodeStr whiteTC[] =
  32.     {
  33. entry( 0, 8, "0011 0101", 0x35),
  34. entry( 1, 6, "00 0111", 0x7),
  35. entry( 2, 4, "0111", 0x7),
  36. entry( 3, 4, "1000", 0x8),
  37. entry( 4, 4, "1011", 0xb),
  38. entry( 5, 4, "1100", 0xc),
  39. entry( 6, 4, "1110", 0xe),
  40. entry( 7, 4, "1111", 0xf),
  41. entry( 8, 5, "1 0011", 0x13),
  42. entry( 9, 5, "1 0100", 0x14),
  43. entry(10, 5, "0 0111", 0x7),
  44. entry(11, 5, "0 1000", 0x8),
  45. entry(12, 6, "00 1000", 0x8),
  46. entry(13, 6, "00 0011", 0x3),
  47. entry(14, 6, "11 0100", 0x34),
  48. entry(15, 6, "11 0101", 0x35),
  49. entry(16, 6, "10 1010", 0x2a),
  50. entry(17, 6, "10 1011", 0x2b),
  51. entry(18, 7, "010 0111", 0x27),
  52. entry(19, 7, "000 1100", 0xc),
  53. entry(20, 7, "000 1000", 0x8),
  54. entry(21, 7, "001 0111", 0x17),
  55. entry(22, 7, "000 0011", 0x3),
  56. entry(23, 7, "000 0100", 0x4),
  57. entry(24, 7, "010 1000", 0x28),
  58. entry(25, 7, "010 1011", 0x2b),
  59. entry(26, 7, "001 0011", 0x13),
  60. entry(27, 7, "010 0100", 0x24),
  61. entry(28, 7, "001 1000", 0x18),
  62. entry(29, 8, "0000 0010", 0x2),
  63. entry(30, 8, "0000 0011", 0x3),
  64. entry(31, 8, "0001 1010", 0x1a),
  65. entry(32, 8, "0001 1011", 0x1b),
  66. entry(33, 8, "0001 0010", 0x12),
  67. entry(34, 8, "0001 0011", 0x13),
  68. entry(35, 8, "0001 0100", 0x14),
  69. entry(36, 8, "0001 0101", 0x15),
  70. entry(37, 8, "0001 0110", 0x16),
  71. entry(38, 8, "0001 0111", 0x17),
  72. entry(39, 8, "0010 1000", 0x28),
  73. entry(40, 8, "0010 1001", 0x29),
  74. entry(41, 8, "0010 1010", 0x2a),
  75. entry(42, 8, "0010 1011", 0x2b),
  76. entry(43, 8, "0010 1100", 0x2c),
  77. entry(44, 8, "0010 1101", 0x2d),
  78. entry(45, 8, "0000 0100", 0x4),
  79. entry(46, 8, "0000 0101", 0x5),
  80. entry(47, 8, "0000 1010", 0xa),
  81. entry(48, 8, "0000 1011", 0xb),
  82. entry(49, 8, "0101 0010", 0x52),
  83. entry(50, 8, "0101 0011", 0x53),
  84. entry(51, 8, "0101 0100", 0x54),
  85. entry(52, 8, "0101 0101", 0x55),
  86. entry(53, 8, "0010 0100", 0x24),
  87. entry(54, 8, "0010 0101", 0x25),
  88. entry(55, 8, "0101 1000", 0x58),
  89. entry(56, 8, "0101 1001", 0x59),
  90. entry(57, 8, "0101 1010", 0x5a),
  91. entry(58, 8, "0101 1011", 0x5b),
  92. entry(59, 8, "0100 1010", 0x4a),
  93. entry(60, 8, "0100 1011", 0x4b),
  94. entry(61, 8, "0011 0010", 0x32),
  95. entry(62, 8, "0011 0011", 0x33),
  96. entry(63, 8, "0011 0100", 0x34)
  97.       };
  98.  
  99.  
  100. static struct CodeStr whiteMUC[] =
  101.     {
  102. entry(  64, 5, "1 1011", 0x1b),
  103. entry( 128, 5, "1 0010", 0x12),
  104. entry( 192, 6, "01 0111", 0x17),
  105. entry( 256, 7, "011 0111", 0x37),
  106. entry( 320, 8, "0011 0110", 0x36),
  107. entry( 384, 8, "0011 0111", 0x37),
  108. entry( 448, 8, "0110 0100", 0x64),
  109. entry( 512, 8, "0110 0101", 0x65),
  110. entry( 576, 8, "0110 1000", 0x68),
  111. entry( 640, 8, "0110 0111", 0x67),
  112. entry( 704, 9, "0 1100 1100", 0xcc),
  113. entry( 768, 9, "0 1100 1101", 0xcd),
  114. entry( 832, 9, "0 1101 0010", 0xd2),
  115. entry( 896, 9, "0 1101 0011", 0xd3),
  116. entry( 960, 9, "0 1101 0100", 0xd4),
  117. entry(1024, 9, "0 1101 0101", 0xd5),
  118. entry(1088, 9, "0 1101 0110", 0xd6),
  119. entry(1152, 9, "0 1101 0111", 0xd7),
  120. entry(1216, 9, "0 1101 1000", 0xd8),
  121. entry(1280, 9, "0 1101 1001", 0xd9),
  122. entry(1344, 9, "0 1101 1010", 0xda),
  123. entry(1408, 9, "0 1101 1011", 0xdb),
  124. entry(1472, 9, "0 1001 1000", 0x98),
  125. entry(1536, 9, "0 1001 1001", 0x99),
  126. entry(1600, 9, "0 1001 1010", 0x9a),
  127. entry(1664, 6, "01 1000", 0x18),
  128. entry(1728, 9, "0 1001 1011", 0x9b),
  129. entry(1792,11, "000 0000 1000", 0x8),
  130. entry(1856,11, "000 0000 1100", 0xc),
  131. entry(1920,11, "000 0000 1101", 0xd),
  132. entry(1984,12, "0000 0001 0010", 0x12),
  133. entry(2048,12, "0000 0001 0011", 0x13),
  134. entry(2112,12, "0000 0001 0100", 0x14),
  135. entry(2176,12, "0000 0001 0101", 0x15),
  136. entry(2240,12, "0000 0001 0110", 0x16),
  137. entry(2304,12, "0000 0001 0111", 0x17),
  138. entry(2368,12, "0000 0001 1100", 0x1c),
  139. entry(2432,12, "0000 0001 1101", 0x1d),
  140. entry(2496,12, "0000 0001 1110", 0x1e),
  141. entry(2560,12, "0000 0001 1111", 0x1f)
  142.       };
  143.  
  144. static struct CodeStr blackTC[] =
  145.     {
  146. entry( 0,10, "00 0011 0111", 0x37),
  147. entry( 1, 3, "010", 0x2),
  148. entry( 2, 2, "11", 0x3),
  149. entry( 3, 2, "10", 0x2),
  150. entry( 4, 3, "011", 0x3),
  151. entry( 5, 4, "0011", 0x3),
  152. entry( 6, 4, "0010", 0x2),
  153. entry( 7, 5, "0 0011", 0x3),
  154. entry( 8, 6, "00 0101", 0x5),
  155. entry( 9, 6, "00 0100", 0x4),
  156. entry(10, 7, "000 0100", 0x4),
  157. entry(11, 7, "000 0101", 0x5),
  158. entry(12, 7, "000 0111", 0x7),
  159. entry(13, 8, "0000 0100", 0x4),
  160. entry(14, 8, "0000 0111", 0x7),
  161. entry(15, 9, "0 0001 1000", 0x18),
  162. entry(16,10, "00 0001 0111", 0x17),
  163. entry(17,10, "00 0001 1000", 0x18),
  164. entry(18,10, "00 0000 1000", 0x8),
  165. entry(19,11, "000 0110 0111", 0x67),
  166. entry(20,11, "000 0110 1000", 0x68),
  167. entry(21,11, "000 0110 1100", 0x6c),
  168. entry(22,11, "000 0011 0111", 0x37),
  169. entry(23,11, "000 0010 1000", 0x28),
  170. entry(24,11, "000 0001 0111", 0x17),
  171. entry(25,11, "000 0001 1000", 0x18),
  172. entry(26,12, "0000 1100 1010", 0xca),
  173. entry(27,12, "0000 1100 1011", 0xcb),
  174. entry(28,12, "0000 1100 1100", 0xcc),
  175. entry(29,12, "0000 1100 1101", 0xcd),
  176. entry(30,12, "0000 0110 1000", 0x68),
  177. entry(31,12, "0000 0110 1001", 0x69),
  178. entry(32,12, "0000 0110 1010", 0x6a),
  179. entry(33,12, "0000 0110 1011", 0x6b),
  180. entry(34,12, "0000 1101 0010", 0xd2),
  181. entry(35,12, "0000 1101 0011", 0xd3),
  182. entry(36,12, "0000 1101 0100", 0xd4),
  183. entry(37,12, "0000 1101 0101", 0xd5),
  184. entry(38,12, "0000 1101 0110", 0xd6),
  185. entry(39,12, "0000 1101 0111", 0xd7),
  186. entry(40,12, "0000 0110 1100", 0x6c),
  187. entry(41,12, "0000 0110 1101", 0x6d),
  188. entry(42,12, "0000 1101 1010", 0xda),
  189. entry(43,12, "0000 1101 1011", 0xdb),
  190. entry(44,12, "0000 0101 0100", 0x54),
  191. entry(45,12, "0000 0101 0101", 0x55),
  192. entry(46,12, "0000 0101 0110", 0x56),
  193. entry(47,12, "0000 0101 0111", 0x57),
  194. entry(48,12, "0000 0110 0100", 0x64),
  195. entry(49,12, "0000 0110 0101", 0x65),
  196. entry(50,12, "0000 0101 0010", 0x52),
  197. entry(51,12, "0000 0101 0011", 0x53),
  198. entry(52,12, "0000 0010 0100", 0x24),
  199. entry(53,12, "0000 0011 0111", 0x37),
  200. entry(54,12, "0000 0011 1000", 0x38),
  201. entry(55,12, "0000 0010 0111", 0x27),
  202. entry(56,12, "0000 0010 1000", 0x28),
  203. entry(57,12, "0000 0101 1000", 0x58),
  204. entry(58,12, "0000 0101 1001", 0x59),
  205. entry(59,12, "0000 0010 1011", 0x2b),
  206. entry(60,12, "0000 0010 1100", 0x2c),
  207. entry(61,12, "0000 0101 1010", 0x5a),
  208. entry(62,12, "0000 0110 0110", 0x66),
  209. entry(63,12, "0000 0110 0111", 0x67)
  210.       };
  211.  
  212. static struct CodeStr blackMUC[] =
  213.     {
  214. entry(  64,10, "00 0000 1111", 0xf),
  215. entry( 128,12, "0000 1100 1000", 0xc8),
  216. entry( 192,12, "0000 1100 1001", 0xc9),
  217. entry( 256,12, "0000 0101 1011", 0x5b),
  218. entry( 320,12, "0000 0011 0011", 0x33),
  219. entry( 384,12, "0000 0011 0100", 0x34),
  220. entry( 448,12, "0000 0011 0101", 0x35),
  221. entry( 512,13, "0 0000 0110 1100", 0x6c),
  222. entry( 576,13, "0 0000 0110 1101", 0x6d),
  223. entry( 640,13, "0 0000 0100 1010", 0x4a),
  224. entry( 704,13, "0 0000 0100 1011", 0x4b),
  225. entry( 768,13, "0 0000 0100 1100", 0x4c),
  226. entry( 832,13, "0 0000 0100 1101", 0x4d),
  227. entry( 896,13, "0 0000 0111 0010", 0x72),
  228. entry( 960,13, "0 0000 0111 0011", 0x73),
  229. entry(1024,13, "0 0000 0111 0100", 0x74),
  230. entry(1088,13, "0 0000 0111 0101", 0x75),
  231. entry(1152,13, "0 0000 0111 0110", 0x76),
  232. entry(1216,13, "0 0000 0111 0111", 0x77),
  233. entry(1280,13, "0 0000 0101 0010", 0x52),
  234. entry(1344,13, "0 0000 0101 0011", 0x53),
  235. entry(1408,13, "0 0000 0101 0100", 0x54),
  236. entry(1472,13, "0 0000 0101 0101", 0x55),
  237. entry(1536,13, "0 0000 0101 1010", 0x5a),
  238. entry(1600,13, "0 0000 0101 1011", 0x5b),
  239. entry(1664,13, "0 0000 0110 0100", 0x64),
  240. entry(1728,13, "0 0000 0110 0101", 0x65),
  241. entry(1792,11, "000 0000 1000", 0x8),
  242. entry(1856,11, "000 0000 1100", 0xc),
  243. entry(1920,11, "000 0000 1101", 0xd),
  244. entry(1984,12, "0000 0001 0010", 0x12),
  245. entry(2048,12, "0000 0001 0011", 0x13),
  246. entry(2112,12, "0000 0001 0100", 0x14),
  247. entry(2176,12, "0000 0001 0101", 0x15),
  248. entry(2240,12, "0000 0001 0110", 0x16),
  249. entry(2304,12, "0000 0001 0111", 0x17),
  250. entry(2368,12, "0000 0001 1100", 0x1c),
  251. entry(2432,12, "0000 0001 1101", 0x1d),
  252. entry(2496,12, "0000 0001 1110", 0x1e),
  253. entry(2560,12, "0000 0001 1111", 0x1f)
  254.       };
  255.  
  256. /* ------------------------------------------------------------------- */
  257.  
  258. OSErr HEncode(srcPtr, dstPtr, imageWidth)
  259. Ptr *srcPtr;
  260. Ptr *dstPtr;
  261. Int16 imageWidth;
  262. {
  263.     Int16 srcBit;
  264.     Int16 dstBit;
  265.     Int8 srcByte;
  266.     UInt8 dstByte;
  267.     Int16 srcBitCount;
  268.     Int16 dstBitCount;
  269.     Boolean whiteRun;
  270.     Int16 index;
  271.     Int16 runLength;
  272.     Int16 bytesLeft;
  273.     Int16 leftToAdd;
  274.     OSErr error;
  275.     Int16 codelength;
  276.     UInt16 pattern;
  277.     UInt16 tempWord;
  278.     Boolean putRun;
  279.     
  280.     leftToAdd = imageWidth;
  281.     bytesLeft = (imageWidth + 7) / 8;
  282.     srcBit = 0;
  283.     dstBit = 0;
  284.     srcBitCount = 0;
  285.     dstBitCount = 0;
  286.     dstByte = 0;
  287.     whiteRun = TRUE;
  288.     error = noErr;
  289.     runLength = 0;
  290.     
  291.     while (leftToAdd > 0)
  292.         {
  293.         if (bytesLeft > 0 && srcBitCount == 0)
  294.             {    /* read a new byte */
  295.             srcByte = *(*srcPtr)++;
  296.             srcBitCount = 8;
  297.             bytesLeft--;
  298.             }
  299.     
  300.         /* if bytesLeft == 0 then no more bytes are available and
  301.         srcBitCount will remain at zero.  This will result in execution
  302.         falling through to the else clause below */
  303.     
  304.         if (srcBitCount == 8 && srcByte == (whiteRun ? 0x0000 : 0xffff))
  305.             {    /* whole byte is continuation of a run */
  306.             runLength += 8;
  307.             srcBitCount = 0;
  308.             }
  309.         else if (srcBitCount > 0 &&
  310.                 ((srcByte >= 0 && whiteRun) || (srcByte < 0 && !whiteRun)))
  311.             {    /* next bit is continuation of a run */
  312.             srcByte = srcByte << 1;
  313.             srcBitCount--;
  314.             runLength++;
  315.             }
  316.         else
  317.             {    /* end of run */
  318.             if (runLength > leftToAdd) runLength = leftToAdd;
  319.             leftToAdd -= runLength;
  320.             putRun = FALSE;
  321.                 while (!putRun)
  322.                     {
  323.                     if (runLength > 2560)
  324.                         {
  325.                         codelength = whiteRun ?
  326.                             whiteMUC[2560 / 64 - 1].codelength :
  327.                             blackMUC[2560 / 64 - 1].codelength;
  328.                         pattern = whiteRun ?
  329.                             whiteMUC[2560 / 64 - 1].pattern :
  330.                             blackMUC[2560 / 64 - 1].pattern;
  331.                         runLength -= 2560;
  332.                         }
  333.                     else if (runLength >= 64)
  334.                         {
  335.                         codelength = whiteRun ?
  336.                             whiteMUC[runLength / 64 - 1].codelength :
  337.                             blackMUC[runLength / 64 - 1].codelength;
  338.                         pattern = whiteRun ?
  339.                             whiteMUC[runLength / 64 - 1].pattern :
  340.                             blackMUC[runLength / 64 - 1].pattern;
  341.                         runLength %= 64;
  342.                         }
  343.                     else
  344.                         {
  345.                         codelength = whiteRun ?
  346.                             whiteTC[runLength].codelength :
  347.                             blackTC[runLength].codelength;
  348.                         pattern = whiteRun ?
  349.                             whiteTC[runLength].pattern :
  350.                             blackTC[runLength].pattern;
  351.                         runLength = 0;
  352.                         putRun = TRUE;
  353.                         }
  354.     
  355.                 while (codelength > 0)
  356.                     {
  357.                     tempWord = dstByte;
  358.                     tempWord |= (pattern >> (8 + dstBitCount));
  359.                     dstByte = tempWord;
  360.                     if (codelength <= 8 - dstBitCount)
  361.                         {
  362.                         dstBitCount += codelength;
  363.                         codelength = 0;
  364.                         }
  365.                     else
  366.                         {
  367.                         codelength -= (8 - dstBitCount);
  368.                         pattern = pattern << (8 - dstBitCount);
  369.                         dstBitCount = 8;
  370.                         }
  371.                     if (dstBitCount == 8)
  372.                         {
  373.                         *(*dstPtr)++ = dstByte;
  374.                         dstByte = 0;
  375.                         dstBitCount = 0;
  376.                         }
  377.                     }
  378.                 }
  379.             whiteRun = !whiteRun;
  380.             }
  381.         }
  382.     
  383.     if (dstBitCount > 0)
  384.         {
  385.         *(*dstPtr)++ = dstByte;
  386.         dstByte = 0;
  387.         dstBitCount = 0;
  388.         }
  389.     
  390.     return(error);
  391. }
  392.  
  393.